package com.ymttest.database.mongo;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.math.RandomUtils;

import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.database.MongoDBDriver;
import com.ymt.util.DataManager;
import com.ymttest.utils.EnvSetup;

/************************************************************************************
 * @File name :UserTokenMatouAppWapper.java
 * @Author : zhouyi
 * @Date : 2015年6月11日
 * @Copyright : 洋码头
 ************************************************************************************/
public class UserTokenMatouAppWapper {
	MongoDBDriver mdd;

	public UserTokenMatouAppWapper() throws Exception {
		mdd = new MongoDBDriver(EnvSetup.getSystemUrl("ymtuser_mongoip"), Integer.valueOf(EnvSetup
				.getSystemUrl("ymtuser_mongoport")), "Ymt_Auth_Token");
		mdd.selectCollection("UserToken_MatouApp");
	}

	/**
	 * 用uid查询AccessToken
	 * 
	 * @param uid
	 * @return AccessToken
	 * @throws Exception
	 */
	public String getAccessTokenByUid(int uid) throws Exception {
		String re = null;
		UserTokenMatouAppBean scmb = null;
		ArrayList<String> list = mdd.selectPartLimit("{\"uId\":" + uid + "}", 1);
		try {
			if (list.size() > 0) {
				re = list.get(0);
				scmb = (UserTokenMatouAppBean) FormatUtil.GSONToObject(re, UserTokenMatouAppBean.class);
				return scmb.getsId();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取指定用户登陆信息token
	 * 
	 * @param part
	 * @return
	 * @throws Exception
	 */
	public UserTokenMatouAppBean getOneAppTokenId(int UserId) throws Exception {
		UserTokenMatouAppBean ati = null;
		ArrayList<String> list = mdd.selectPartLimit("{'uId':" + UserId + "}", 1);
		try {
			ati = (UserTokenMatouAppBean) FormatUtil.GSONToObject(list.get(0), UserTokenMatouAppBean.class);
		} catch (Exception e) {
			Logger.debug("查询用户信息失败");
			e.printStackTrace();
		}
		return ati;
	}

	/**
	 * 获取随机token
	 * 
	 * @param part
	 * @return
	 * @throws Exception
	 */
	public String getRandomAppTokenId(String part) throws Exception {
		String re = null;
		UserTokenMatouAppBean ati = null;
		int TotalNum = mdd.selectCount(part);
		int tempNum = RandomUtils.nextInt(TotalNum);
		while (tempNum == 0) {
			tempNum = RandomUtils.nextInt(TotalNum);
		}
		ArrayList<String> list = mdd.selectPartLimit(part, tempNum);
		try {
			re = list.get(tempNum - 1);
			ati = (UserTokenMatouAppBean) FormatUtil.GSONToObject(re, UserTokenMatouAppBean.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String TokenId = ati.getsId();
		return TokenId;
	}

	public static void main(String args[]) {
		try {
			List<String> uidlist = DataManager.getData("uid.dat");
			UserTokenMatouAppWapper ut = new UserTokenMatouAppWapper();
			for (String uid : uidlist) {
				String ac = ut.getAccessTokenByUid(Integer.valueOf(uid));
				if (ac != null) {
					DataManager.appendFileToLien("AccessToken.dat", ac);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	protected void close() throws Throwable {
		mdd.close();
	}
}
